<div id="games">
</div>

<table style="width: 100%"><tr>
    <td style="width: 120px; vertical-align: top;" id="type-menus">
        <div class="mt">玩法分类</div>
        <div class="list">
        </div>
    </td>

    <td style="width: 700px; vertical-align: top;">
        <table id="lottery">
            <thead>
                <tr>
                    <th colspan="1">玩法</th>
                    <th width="120px">赔率</th>
                    <th>退水</th>
                    <th>单注最低</th>
                    <th>单注最高</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </td>

    <td style="padding: 10px 0px 0px 10px; vertical-align: top;">
        <div class="title5">单期限额</div>
        <div class="content5">
            <input type="number" class="am-form-field game-max" placeholder="最大限额">
            <button type="button" class="btn-issue-limit">保存</button>
            <div class="clue">说明：不填或者填写0表示不限制。</div>
        </div>

        <div class="title5">单注限额</div>
        <div class="content5">
            <input type="number" class="am-form-field quick-min" placeholder="最小限额">
            <input type="number" class="am-form-field quick-max" placeholder="最大限额">
            <button type="button" class="btn-quick">快速设置</button>
        </div>
    </td>
</table>
<div id="game-page"></div>

<div class="button_line">
    <button class="am-btn am-btn-primary btn-save admin_button">保存</button>
</div>

<style type="text/css">
    #lottery {
        width: 100%;
        border-right: 1px solid #ccc;
        border-bottom: 1px solid #ccc;
        margin-top: 10px;
    }
    #lottery td, #lottery th {
        border-left: 1px solid #ccc;
        border-top: 1px solid #ccc;
        text-align: center;
        vertical-align: middle;
    }
    #lottery td.title {
        width: 100px;
    }
    #lottery th {
        line-height: 28px;
        background-color: #eee;
    }
    #lottery input {
        text-align: center;
    }

    #games a {
        display: inline-block;
        margin: 5px;
        text-align: center;
        color: #6e706d;
        font-size: 14px;
        background-color: #eee;
        width: 100px;
        line-height: 24px;
        border-radius: 5px;
    }
    #games a.selected {
        background-color: #65f2fc;
        color: #000;
    }

    div.title5 {
        font-size: 14px;
        font-weight: bold;
        color: #000;
        line-height: 30px;
    }
    div.content5 {
        padding: 10px;
        min-width: 120px;
    }
    div.content5 .clue { color: red; line-height: 16px; }
    .content5 button {
        background-color: #13a7a9;
        border: 0px;
        border-radius: 3px;
        line-height: 26px;
        padding: 0px 8px 0px 8px;
        min-width: 80px;
        color: #fff;
        margin-top: 10px;
    }

    #type-menus .mt {
        text-align: center;
        font-size: 14px;
        color: #000;
        border-bottom:1px solid #ccc;
        line-height: 40px;
        margin-bottom: 8px;
    }
    #type-menus a {
        display: block;
        text-align: center;
        width: 100px;
        line-height: 28px;
        background-color: #ccc;
        border-radius: 3px;
        font-size: 13px;
        margin: auto;
        margin-bottom: 10px;
        color: #000;
    }
    #type-menus a.active {
        background: #13a7a9;
        color: #fff;
    }
</style>

<script src="../public/all/js/pages/lottery_info.js"></script>
<script>
    (function() {
        var _loadGame = function(g) {
            $.get("../admin/pages/admin/lottery/" + g + "_lottery.html", function(r) {
                $("#game-page").empty();
                $("#game-page").html(r);
            });
        }

        GroupUtil.init(false, function() {
            _loadGame(GroupUtil.group.lottery);
        });
    })();

    function fillTypeMenus() {
        var arr = $("#lottery td.classify");
        var div = $("#type-menus .list"); div.empty();
        for (var i = 0; i < arr.length; i++) {
            div.append('<a href="#">' + $(arr[i]).html() + '</a>');
        }
        var selectType = $(arr[0]).html();
        var f = function(selectType) {
            var arr = $("#type-menus a");
            arr.removeClass("active");
            for (var i in arr) {
                if ($(arr[i]).html() == selectType) {
                    $(arr[i]).addClass("active");
                    break;
                }
            }

            // 非本类的行都隐藏掉
            var arr = $("#lottery tbody tr");
            arr.hide();
            for (var i = 0; i < arr.length; i++) {
                var tr = $(arr[i]);
                if (tr.find(".classify").html() != selectType) continue;

                // 显示出来
                var rows = parseInt(tr.find(".classify").attr("rowspan"));
                for (var j = i; j < i + rows; j++) {
                    $(arr[j]).show();
                }
                break;
            }
        }
        f(selectType);
        $("#type-menus a").click(function(e) {
            f($(e.currentTarget).html());
        });
    }

    function updateGameLottery(g) {
        fillTypeMenus();

        startLoading();
        CMD.query_lottery_rate(GroupUtil.group.lottery, GroupUtil.group.id, function(r) {
            stopLoading();
            var m = {};
            for (var i in r) {
                m[r[i].target] = r[i];
            }
            _fillLottery(g, m);
        });
    }

    function _fillLottery(g, list) {
        if (list.targets) {
            $(".game-max").val(list.targets.max/100);
        }
        
        var rule = Util.getGameRule(g);
        var map = rule.map;
        for (var k in list) {
            if (isEmpty(map[k])) {
                continue;
            }
            if (list[k].value)
                map[k].rate = list[k].value;
            if (list[k].back)
                map[k].back = list[k].back;
            if (list[k].min) map[k].min = list[k].min;
            if (list[k].max) map[k].max = list[k].max;
        }

        var trs = $("#lottery tbody tr");
        for (var i = 0; i < trs.length; i++) {
            var k = $(trs[i]).attr("key");
            if (!k) continue;
            if (!map[k]) {
                console.log("不存在类型", k);
                continue;
            }

            $(trs[i]).find(".rate input").val(map[k].rate);

            if (!map[k].back) $(trs[i]).find(".back input").val(0);
            else $(trs[i]).find(".back input").val(100 * map[k].back);

            if (!map[k].min) $(trs[i]).find(".min input").val(0);
            else $(trs[i]).find(".min input").val(map[k].min / 100);

            if (!map[k].max) $(trs[i]).find(".max input").val(0);
            else $(trs[i]).find(".max input").val(map[k].max / 100);
        }
    }

    $(".button_line button").click(function() {
        var m1 = {}, m2 = {}, m3 = {};
        var trs = $("#lottery tbody tr");
        for (var i = 0; i < trs.length; i++) {
            var k = $(trs[i]).attr("key");
            if (!k) continue;

            m1[k] = $(trs[i]).find(".rate input").val() || 1;
            m2[k] = $(trs[i]).find(".back input").val() || 0;
            m2[k] = parseFloat(m2[k]) / 100;
        
            var v1 = $(trs[i]).find(".min input").val() || 0;
            v1 = v1 * 100;
            var v2 = $(trs[i]).find(".max input").val() || 0;
            v2 = v2 * 100;
            m3[k] = v1 + "," + v2;
        }

        var g = $("#games a.selected").attr("value");
        console.log("赔率", m1);
        startLoading();
        CMD.group_lottery_rate_save(GroupUtil.group.lottery, GroupUtil.group.id, m1, function() {
            stopLoading();
            alert2("保存成功。");
        });

        console.log("返水", m2);
        CMD.group_lottery_back_save(GroupUtil.group.lottery, GroupUtil.group.id, m2, function() {
        });

        console.log("限额", m3);
        CMD.group_lottery_rate_ranges(GroupUtil.group.lottery, GroupUtil.group.id, m3, function() {
        });
    });

    $(".btn-quick").click(function() {
        var v1 = $(".quick-min").val(), v2 = $(".quick-max").val();
        var arr = $("#lottery tbody tr");
        for (var i = 0; i < arr.length; i++) {
            if ($(arr[i]).css("display") == "none") continue;
            $(arr[i]).find(".min input").val(v1);
            $(arr[i]).find(".max input").val(v2);
        }
    });
    $(".btn-issue-limit").click(function() {
        var v = $(".game-max").val().trim();
        if (v == '') {
            alert2("请填入最大单期限额的金额（单位：元）！", true);
            return;
        }

        v = parseInt(v);
        if (v < 0) {
            alert2("最大限额应该大于等于0！", true);
            return;
        }

        var g = $("#games a.selected").attr("value");
        CMD.group_lottery_rate_ranges(GroupUtil.group.lottery, GroupUtil.group.id, {
            targets: "0," + (v * 100),
        }, function() {
            alert2("修改完成！");
        });
    });
</script>
